#include "unp.h"


int main(int argc , char *argv[])
{

  int c , flags;
  mqd_t mqd;
  struct mq_attr attr;

  flags  = O_RDWR | O_CREAT;
 
  while((c=getopt(argc,argv,"em:z:")) !=-1){
     switch(c){
        case 'e':
          flags = flags | O_EXCL;
          break;
        case 'm':
	  attr.mq_maxmsg = atol(optarg);
          break; 
        case 'z':
	  attr.mq_msgsize =atol(optarg);
          break;
     }
  }


  if(optind != (argc-1)){
      err_quit("usage: mq_create [-e] [-m maxmsg -z msgsize] <name>");
      return -1;
  }

  printf("name:%s\n",argv[optind]);

  if( (attr.mq_maxmsg != 0 && attr.mq_msgsize == 0) || 
      (attr.mq_maxmsg == 0 && attr.mq_msgsize != 0) )
      err_quit("must set both m and z arguments");

  mqd = mq_open(argv[optind],flags,FILE_MODE,
             attr.mq_maxmsg>0?&attr:NULL);
  
  if(mqd == -1){
     //printf("mq_open error:%d\n",(errno));
     err_sys("mq_open error");
     return errno;
  }else{
     err_msg("mq_open success:%d\n",mqd);
  }
  
  mq_close(mqd);
  return 0;
}
